import Vue from 'vue'
import Router from 'vue-router'


Vue.use(Router)

const router = new Router({
  routes: [
    //默认打开
    {
      path: '/',
      redirect: '/login',
    },

    {
      path: '/login',
      name: 'login',
      component: resolve => require(['@/components/Login.vue'], resolve),
      meta: {
        title: '登录页',
      },
    },
    {
      path: '*',
      redirect: '/404'
    },
    {
      path: '/home',
      name: 'home',
      component: resolve => require(['@/components/Home.vue'], resolve),
      meta: {
        title: '首页',
      },
      children: [
        /*运营商模块*/
        {
          path: '/operator_type',
          component: resolve => require(['@/components/operator/operator_type/operator_type'], resolve),
          meta: {
            title: '运营商模块口罩机分类管理'
          }
        },
        {
          path: '/operator_publiAttr',
          component: resolve => require(['@/components/operator/operator_publiAttr/operator_publiAttr'], resolve),
          meta: {
            title: '运营商模块口罩机公共属性管理'
          }
        },
        {
          path: '/operator_manufacturer',
          component: resolve => require(['@/components/operator/operator_manufacturer/operator_manufacturer'], resolve),
          meta: {
            title: '运营商模块口罩机制造商管理'
          }
        },
        {
          path: '/operator_producer',
          component: resolve => require(['@/components/operator/operator_producer/operator_producer'], resolve),
          meta: {
            title: '运营商模块口罩机生产商管理'
          }
        },
        {
          path: '/operator_model',
          component: resolve => require(['@/components/operator/operator_model/operator_model'], resolve),
          meta: {
            title: '运营商模块模型管理'
          }
        },
        {
          path: '/operator_variable',
          component: resolve => require(['@/components/operator/operator_variable'], resolve),
          meta: {
            title: '运营商模块口罩变量管理'
          }
        },
        {
          path: '/operator_mask',
          component: resolve => require(['@/components/operator/operator_mask/operator_mask'], resolve),
          meta: {
            title: '运营商模块口罩机管理'
          }
        },
        {
          path: '/operator_record',
          component: resolve => require(['@/components/operator/operator_record'], resolve),
          meta: {
            title: '运营商模块产能报表'
          }
        },
        {
          path: '/operator_account',
          component: resolve => require(['@/components/operator/operator_account/operator_account'], resolve),
          meta: {
            title: '运营商模块账号管理'
          }
        },
        {
          path: '/testupload',
          component: resolve => require(['@/components/test/upload1'], resolve),
          meta: {
            title: '测试上传图片'
          }
        },
        // {
        //   path: '/testupload',
        //   component: resolve => require(['@/components/test/uploadVedio'], resolve),
        //   meta: {
        //     title: '测试上传图片'
        //   }
        // },
        
      ]
    }
  ]
})
/**
 * 重写路由的push方法
 */
const routerPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error => error)
}

//全局路由守卫
router.beforeEach((to, from, next) => {
  console.log('navigation-guards');
  // to: Route: 即将要进入的目标 路由对象
  // from: Route: 当前导航正要离开的路由
  // next: Function: 一定要调用该方法来 resolve 这个钩子。执行效果依赖 next 方法的调用参数。
  let isLogin = localStorage.getItem('hasLogin'); // 是否登录
  console.log("状态", isLogin)
  const nextRoute = ['home', 'warnMessage'];

  // 未登录状态；当路由到nextRoute指定页时，跳转至login
  if (nextRoute.indexOf(to.name) >= 0) {
    if (isLogin == 'false') {
      router.push({ name: 'login' })
    }
  }
  // 已登录状态；当路由到login时，跳转至home 
  if (to.name === 'login') {
    console.log(111)
    if (isLogin == 'true') {
      console.log(122)
      router.push({ name: 'home' });
    }
  }
  next();
});
export default router;
